﻿using System;
using System.IO;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Threading;
using System.Globalization;
using System.Collections.Specialized;
using FreeTextBoxControls;
using FreeTextBoxControls.Support;
using MyWebPagesStarterKit.Utils;

public partial class Administration_UIControls_HtmlEdit : System.Web.UI.UserControl
{
    private FreeTextBox ftbEditor;

    protected override void OnInit(EventArgs e)
    {
        ftbEditor = new FreeTextBox();
        ftbEditor.DesignModeCss = "~/Handlers/Css.ashx";
        ftbEditor.Width = Unit.Percentage(100.0);

        //Prepare for administration of buttons. "Creating sets of buttons"
        ftbEditor.ToolbarLayout = "ParagraphMenu,FontSizesMenu,FontForeColorsMenu,FontForeColorPicker,FontBackColorsMenu,FontBackColorPicker|Bold,Italic,Underline,Strikethrough,Superscript,Subscript,RemoveFormat|Undo,Redo|JustifyLeft,JustifyRight,JustifyCenter,JustifyFull;BulletedList,NumberedList,Indent,Outdent;CreateLink,Unlink,InsertImageFromGallery|InsertTable,EditTable;InsertTableRowAfter,InsertTableRowBefore,DeleteTableRow;InsertTableColumnAfter,InsertTableColumnBefore,DeleteTableColumn|Preview";

        string galleryPath = ResolveUrl("~/App_Data/UserImages/Image");

        if (!Directory.Exists(galleryPath))
            Directory.CreateDirectory(galleryPath);


        ftbEditor.ImageGalleryUrl = ResolveUrl(string.Format("~/FreeTextBox/ftb.imagegallery.aspx?rif={0}&cif={0}", galleryPath));
        ftbEditor.ImageGalleryPath = galleryPath;

        ftbEditor.TextDirection = Thread.CurrentThread.CurrentCulture.TextInfo.IsRightToLeft ? TextDirection.RightToLeft : TextDirection.LeftToRight;

        switch (Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName.ToLower())
        {
            case "ar":
                ftbEditor.Language = "ar-SA";
                break;
            case "cs":
                ftbEditor.Language = "cs-CZ";
                break;
            case "de":
                ftbEditor.Language = "de-DE";
                break;
            case "el":
                ftbEditor.Language = "el-GR";
                break;
            case "es":
                ftbEditor.Language = "es-ES";
                break;
            case "fr":
                ftbEditor.Language = "fr-FR";
                break;
            case "nl":
                ftbEditor.Language = "nl-NL";
                break;
            case "pl":
                ftbEditor.Language = "pl-PL";
                break;
            case "pt":
                ftbEditor.Language = "pt-PT";
                break;
            case "ru":
                ftbEditor.Language = "ru-RU";
                break;
            case "sk":
                //use czech for now, as FTB does not contain a localization for slovak at the moment.
                ftbEditor.Language = "cs-CZ";
                //ftbEditor.Language = "sk-SK";
                break;
            case "sv":
                ftbEditor.Language = "sv-SE";
                break;
            case "zh":
                if (Thread.CurrentThread.CurrentUICulture.IetfLanguageTag.ToLower() == "zh-tw")
                    ftbEditor.Language = "zh-TW";
                else
                    ftbEditor.Language = "zh-CN";
                break;
            default:
                ftbEditor.Language = "en-US";
                break;
        }

        ctl.Controls.Add(ftbEditor);
        base.OnInit(e);
    }


    /// <summary>
    /// Generates and set Xhtml output to any text object
    /// </summary>
    public string Text
    {
        get { return GenerateXhtml(ftbEditor.Text); }
        set { ftbEditor.Text = GenerateXhtml(value); }
    }

    /// <summary>
    /// The free version of freetextbox dont provide Xhtml. This is overriden with a XhtmlHelper class.
    /// </summary>
    /// <param name="source">The source to check</param>
    /// <returns>A Xhtml valid source</returns>
    private string GenerateXhtml(string source)
    {
        return new XhtmlHelper(source).ToString();
    }
}
